package controllers;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.mail.EmailException;

import models.Idea;
import models.Tag;
import models.Topic;
import models.User;

import play.mvc.Controller;
import play.mvc.With;
import play.*;

@With(Secure.class)
public class Ttags extends Controller {
	/**
	 * Tag Home Page
	 * 
	 * @author Ibrahim Kessba
	 * 
	 **/
	public static void tagHome() {
		render();
	}

	public static void viewAllTags() {

		List allTagsTempList = models.Tag.findAll();
		render(allTagsTempList);

	}

	public static void newTag(String tagName) {

		Tag tempTag = new Tag();

		boolean flag = Tag.isTagFound(tagName/* , testingUserId */);

		if (!flag) {

			tempTag.setName(tagName);
			tempTag.save();

			tagHome();

		} else {

			System.out
					.println("\n.....\n"
							+ "Tag : "
							+ " Already have been Found in the System \n You will be redicted to try Another name"
							+ "\n.....\n");
			Application.index();
		}

		tagHome();

	}

	public static void tagShow(long tagID) {

		Tag temp = Tag.findById(tagID);

		String tagName = temp.getName();

		String status = temp.getStatus();

		/*
		 * String tagOwner = temp.getOwner().getfName();
		 */
		render(temp/* , tagOwner */);

	}

	public static void tagNewForm() {
		String username = Security.connected();
		User user = User.findByUsername(username);
		render(user);

	}
	/**
	 * checks if the idea belongs to the user and creates a tag for it
	 * 
	 * @author nora zakhary
	 * @version 2011.0424
	 * @category C3S5
	 * @param String name, 
			  long newlyTaggedIdeaId
			  
	 * @return void
	 * @throws EmailException
	 */

	/**
	 * this part was made to notify the followers of a tag when a change
	 * happen
	 * 
	 * @author Mostafa Badr
	 * @version 2010.05.05
	 * @category C2S21
	 * 
	 */
	public static void createTagForIdea(String name, 
			long newlyTaggedIdeaId) throws EmailException {

		String username = Security.connected();
		User user = User.find("byUsername", username).first();
		Tag createNewTag;
		Idea i = Idea.findById(newlyTaggedIdeaId);

		LinkedList<Idea> taggedIdeas = new LinkedList<Idea>();
		String x;
		Tag t = Tag.find("byName", name).first();
		System.out.println(user.getIdeaList().get(0).getId());
          
		if (
			  user.getIdeaList().contains(i) &&
			 t == null) {
			System.out.println("tag is being created");

			taggedIdeas.add(i);

			createNewTag = new Tag(user, name, "open", taggedIdeas);
			createNewTag.save();
			i.getTags().add(createNewTag);
			i.save();
			

			x = "tagCreated";
		}
	 
		 		
		else{	if (t != null) {
					System.out.println("tag is found");
					x = "alreadyFound";
				}
   
				else {
					x = "notEnoughprivilages";
				}
			
		}
		// ------------------ by mostafa badr
	/*	Tag temp = new Tag();
		if (Tag.find("alreadyFound", name) != null) {
			temp = new Tag(user, name, "open", taggedIdeas);
			if (temp.getFollowers() != null) {
				long idd = temp.getOwner().getId();
				Notifications
						.notifyMany(
								idd,
								notifyFollowersOfTag(temp.getFollowers()),
								"The Tag you are following have been added to a new idea",
								"   ");
			}
		}
		// ---------*/
		render(name,newlyTaggedIdeaId,x);

	}
	/**
	 * just put the followers of a tag in a long
	 * 
	 * @author Mostafa Badr
	 * @version 2010.0405
	 * @category C2S21
	 * @params LinkedList of the followers of the tag
	 * @return void
	 */

	public static long[] notifyFollowersOfTag(LinkedList<User> followers) {
		long[] users = null; // not
		for (int j = 0; j < followers.size(); j++) {
			users[j] = followers.get(j).getId();
		}
		return users;
	}

	/**
	 * checks if the user is the creator of the Idea and takes the tag as an input and adds it to the Idea
	 * 
	 * @author nora zakhary
	 * @version 2011.04.30
	 * @category C3S4
	 * @param String tag, 
	 *        long taggedIdeaId
	 * @return void
	 * @throws EmailException 
	 */
	
	/**
	 * this part was made to notify the followers of a tag when a change
	 * happen
	 * 
	 * @author Mostafa Badr
	 * @version 2010.05.05
	 * @category C2S21
	 * 
	 */
	public static void tagMyIdeas( String tag, long taggedIdeaId) throws EmailException {

		String x = "";
		String username = Security.connected();
		User user = User.find("byUsername", username).first();

		Idea i = Idea.findById(taggedIdeaId);
		Tag t = Tag.find("byName", tag).first();

		if ( user.getIdeaList().contains(i)) {

			if (t != null && !i.getTags().contains(t)) {

				t.getTaggedIdeas().add(i);

				x = "tagAdded";
			}
			if (t != null && i.getTags().contains(t)) {
				x = "AlreadyFound";
			} else if (t == null) {
				x = "NotFound";
			}
		} else {
			x = "NotEnoughPrivilages";
		}

		// ------------------

		/*

		if (x == "tagAdded") {
			Tag temp = new Tag();
			if (Tag.find("alreadyFound", tag) != null) {
				temp = Tag.find("ByName", tag).first();
				if (temp.getFollowers() != null) {
					long idd = temp.getOwner().getId();
					Notifications
							.notifyMany(idd,notifyFollowersOfTag(temp.getFollowers()),
									"The Tag you are following have been added to a new idea",
									"   ");
				}
			}
		}

		// ---------*/

		render(tag,taggedIdeaId,x);
	}

	/**
	 *checks if the user is the creator of the topic and takes the tag as an input and adds it to the topic 
	 * 
	 * @author nora zakhary
	 * @version 2011.04.30
	 * @category C3S3
	 * @params String tag,
	 *         Topic taggedTopicId
	 * @return void
	 * @throws EmailException 
	 */
	/**
	 * this part was made to notify the followers of a tag when a change
	 * happen
	 * 
	 * @author Mostafa Badr
	 * @version 2010.05.05
	 * @category C2S21
	 * 
	 */

	public static void tagMyTopics( String tag, Topic taggedTopicId) throws EmailException {

		String x = "";
		String username = Security.connected();
		User user = User.find("byUsername", username).first();

		Topic topic = Topic.findById(taggedTopicId);
		Tag t = Tag.find("byName", tag).first();

		if (user.getIdeaList().contains(topic)) {

			if (t != null && !topic.getTags().contains(t)) {

				t.getTaggedTopics().add(topic);

				x = "tagAdded";
			}
			if (t != null && topic.getTags().contains(t)) {
				x = "AlreadyFound";
			} else if (t == null) {
				x = "NotFound";
			}
		} else {
			x = "NotEnoughPrivilages";
		}
		
		
		// ------------------mostafa
/*
	
		if (x == "tagAdded") {
			Tag temp = new Tag();
			if (Tag.find("alreadyFound", tag) != null) {
				temp = Tag.find("ByName", tag).first();
				if (temp.getFollowers() != null) {
					long idd = temp.getOwner().getId();
					Notifications.notifyMany(idd,notifyFollowersOfTag(temp.getFollowers()),
									"The Tag you are following have been added to a new topic",
									"   ");
				}
			}
		}

		// ---------*/
		
		render(tag,taggedTopicId,x);
	}
}
